package fibo

import (
	"errors"
	"math/big"
)

var list []*big.Int

func Fibonacci(num int, sign byte) (res []*big.Int, err error) {
	if num < 0 {
		num = 1
	}
	if num > 100 {
		err = errors.New("fibonacci number must not great than 100")
		return
	}
	if sign != '+' && sign != '*' {
		err = errors.New("operator sign must be + or *")
		return
	}
	// 扩容
	len := If(num > 2, num, 2).(int)
	list = make([]*big.Int, len)
	if sign == '+' {
		addFibo(list)
	} else {
		mulFibo(list)
	}
	res = list[0:num]

	return
}

func addFibo(li []*big.Int) {
	li[0] = big.NewInt(1)
	li[1] = big.NewInt(1)
	for i := 2; i < len(li); i++ {
		li[i] = big.NewInt(1)
		li[i].Add(li[i-1], li[i-2])
	}
}

func mulFibo(li []*big.Int) {
	li[0] = big.NewInt(1)
	li[1] = big.NewInt(2)
	for i := 2; i < len(li); i++ {
		li[i] = big.NewInt(1)
		li[i].Mul(li[i-1], li[i-2])
	}
}

func If(cond bool, a, b interface{}) interface{} {
	if cond {
		return a
	}
	return b
}
